<template>
<view class="contain flex flex-direction">
		<view class="auth_top">
			 <!-- @click="wxLoginOut" -->
			<view class="logoBox">
				<view class="logo felx justify-center align-center"><text class="acon-icon-test1"></text></view>
			</view>
			<view class="comText">
				欢迎来到
			</view>
			<view class="welText">小颐有约</view>
			<view class="welDis">为人类健康事业服务</view>
		</view>
		<block>
			<!-- <view v-if="canIUser"> -->
			<!--  <view v-if="canIUser" class="authFooteSection">-->
			<!--微信授权start-->
			<view v-if="canIUser" class="authFooteSection">
				<button v-if="canIUseGetUserProfile" class="nextStep login-wechat flex align-center justify-center"  @tap="wxGetUserProfile" withCredentials="true" lang="zh_CN">
					<view>
						<text class="econwechat"></text>
						<text class="margin-left-xs">快捷登录</text>
					</view>
				</button>
				<button v-else class="nextStep login-wechat flex align-center justify-center" open-type="getUserInfo" @getuserinfo="wxGetUserInfo" withCredentials="true" lang="zh_CN">
					<view>
						<text class="econwechat"></text>
						<text class="margin-left-xs">快捷登录</text>
					</view>
				</button>
				<view class="cancel" @tap="goBackPage()" >
					取消
				</view>
				<view class="flex tips align-center" @tap="toggleTips()">
					<text class="tipIco" :class="[toggClass?'cuIcon-squarecheck':'cuIcon-square']"></text>
					<text class="text-c9 margin-left-xs">点击“快捷登录”即表示您同意 </text>
					<text class="text-green" @tap.stop="showModalService">《小颐有约用户服务条款》</text>
				</view>
			</view>
			<!--微信授权end-->
			<!--微信登录start-->
			<view v-else class="loginFooteSection">
				<view class="btnBox flex justify-between">
					<button class="nextStep login-wechat flex align-center justify-center" open-type="getPhoneNumber" @getphonenumber="wxGetPhoneInfo"   withCredentials="true" lang="zh_CN">
						<view>
							<text class="econwechat"></text>
							<text class="margin-left-xs">登录</text>
						</view>
					</button>
					<view class="cancel" @tap="goBackPage()" >
						取消
					</view>
				</view>
				<view class="flex tips align-center" @tap="toggleTips()">
					<text class="tipIco" :class="[toggClass?'cuIcon-squarecheck':'cuIcon-square']"></text>
					<text class="text-c9 margin-left-xs">点击“快捷登录”即表示您同意 </text>
					<text class="text-green" @tap.stop="showModalService">《小颐有约用户服务条款》</text>
				</view>
			</view>
			<!--微信登录end-->
			<!-- <view class="nextStep" >请升级微信版本</view> -->
		</block>
		<!-- 服务条款弹窗 -->
		<view class="cu-modal zindex" :class="modalSName === 'show' ? 'show' : ''" @tap="hideModalService">
			<view class="cu-dialog serv" @tap.stop="stopPrevent">
				<view class="servContent" @tap="hideModalService">
					<view class="sboxMain">
						<rich-text :nodes="data.richT"></rich-text>
						<!-- <view class="stit">
							欢迎您阅读....
						</view>
						<view class="scon">
							本《颐而康精选用户服务协议》（以下简称“本服务协议”）本平台上销售商展示的商品和价格等信息仅仅是要约邀请，用户下单时须填写希望购买的商品数量、价款及支付方式、收货人、联系方式、收货地址（合同履行地点）、合同履行方式等内容；系统生成的订单信息是计算机信息系统根据用户填写的内容自动生成的数据，仅是用户向销售商发出的合同要约；销售商收到用户的订单信息后，只有在销售商将用户在订单中订购的商品从仓库实际直接向用户发出时（以商品出库为标志），方视为用户与销售商之间就实际直接向用户发出的商品建立了合同关系；如果用户在一份订单里订购了多种商品并且销售商只给用户发出了部分商品时，用户与销售商之间仅就实际直接向用户发出的商品建立了合同关系，只有在销售商实际直接向用户发出了订单中订购的其他商品时，用户和销售商之间就订单中其他已实际直接向用户发出的商品才成立合同关系。当用户作为消费者为生活消费需要下单并支付货款的情况下，用户货款支付成功后即视为用户与销售商之间就已支付货款部分的订单建立了合同关系。
						</view> -->
					</view>
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="{show:showAgr}" @click="showAgr=false">
			<view class="cu-dialog agr" @tap.stop="stopPrevent">
				<view class="agrContent padding">
					<view class="text-xl text-c0">
						服务协议及隐私保护
					</view>
					<view class="padding-top text-left">
						为了更好的保障您的合法权益，请您阅读并同意以下协议<text class="text-ngreen" @click.stop="showModalService">《小颐有约用户服务条款》</text>。
					</view>
					<view class="flex padding-top-lg align-center justify-between text-ngreen text-xl">
						<view class="agrbtn" @click.stop="showAgr=false">
							不同意
						</view>
						<button v-if="canIUseGetUserProfile" class="agrbtn full"  @click.stop="wxGetUserProfile('model')" withCredentials="true" lang="zh_CN">
							同 意
						</button>
						<button v-else class="agrbtn full" open-type="getUserInfo" @getuserinfo="wxGetUserInfo($event,'model')" withCredentials="true" lang="zh_CN">
							同 意
						</button>
						<!-- <view class="agrbtn full">
							同 意
						</view> -->
					</view>
				</view>
			</view>
		</view>
		<tb-privacy :onNeed='true'></tb-privacy>
		<loading></loading>
	</view>
</template>

<script lang="ts" setup>
	import { ref } from "vue";
	import  data  from './xkjAgreement'
	import { onLoad ,onUnload} from '@dcloudio/uni-app';
	import { mnavTo,showLoading, hideLoading } from '@/utils/helper';
	import mapi from '@/utils/mapi';
	import { env } from '@/api/env.js';
	import global from '@/utils/global';
	import { wx_fPhone,getMemberLoginMst,registerMembert,getServerLogint,wxLoginOutt,member_tikett } from '@/api/vmeitime-http/index.js'
	import { useUserStore } from '@/stores/commonStore';
	const userStore = useUserStore();
	const lastAuthTime=ref(0)
	const lastPTime=ref(0)
	const userPhone=ref('')
	const canTapLogin=ref(true)
	const toggClass=ref(false)
	const wxCode=ref('')
	const canIUser=ref(true)
	const canIUseGetUserProfile=ref<any>('')
	const user_content=ref('')
	const userTiket=ref('')
	const userParams=ref('')
	const archDefaultMsg=ref('')
	const agrvalue=ref(false)
	const isLogin=ref('')
	const modalSName=ref('')
	const shopData=ref<any>({})
	const showAgr=ref(false)
	onLoad((items)=>{
		isLogin.value = uni.getStorageSync(global.IS_LOGIN)||'';
		wxCode.value = '';
		shopData.value = uni.getStorageSync(global.SHOP_CURRENT_ID)||{};//this.shopData.nmember_id
		canIUseGetUserProfile.value = wx.getUserProfile?true:false;
		getCode();
	})
	onUnload(()=>{
		canIUser.value = true
		hideLoading();
		global.CENTER_LOGIN_PAGE = false; //表示退出Page
	})
	const wxGetUserProfile=(source:any='')=>{
		console.log('wxGetUserProfile+',source)
		if(!toggClass.value&&source!='model'){
		  // this.$api.msg('请先同意"用户服务条款"再操作哦');
		  showAgr.value = true
		  canTapLogin.value = true
		  return;
		}
		if(source=='model'){
			toggClass.value=true
			showAgr.value = false
		}
		
		if(!canTapLogin.value){return;}
		let currentAuthTime = new Date().getTime();//防止重复提交
		// console.log('wxGetUserProfile_showAuthorize',this.showAuthorize)
		let timeDif = currentAuthTime - lastAuthTime.value;
		if (timeDif < 1000) {//触发时间短于1s
			return;
		}
		if (wxCode.value&&(timeDif > 1000*60*5)) {//触发时间短于1s
			getCode();
		}
		lastAuthTime.value = currentAuthTime;
		canTapLogin.value = false;
		// console.log('wxCode007',that.wxCode)
		if(wxCode.value){
			getUserProfile();
		}else{
			canTapLogin.value = true;
			getCode();
		}
	}
	const getUserProfile=()=>{
		if(!toggClass.value){
		  mapi.msg('请先同意"用户服务条款"再操作哦');
		  canTapLogin.value = true
		  return;
		}
		//暂
		// that.$showLoading({
		// 	mask: true,
		// 	title: ''
		// })
		wx.getUserProfile({
		    desc: "获取你的昵称、头像、地区及性别",
		    success: res => {
				hideLoading();
		      console.log('获取你的昵称',res)
				let wxUserInfo = res;
				let {userInfo,iv,encryptedData} = wxUserInfo;
				uni.setStorageSync(global.WX_USER_INFO, userInfo);
				const loginData = {
					code: wxCode.value,
					// appid: that.$global.APP_ID,
					iv: iv,
					shopId:'10000459',
					encryptedData: encryptedData
				};
				getMainLogin(loginData);
		    },
		    fail: res => {
		    	 //拒绝授权
				hideLoading();
				canTapLogin.value = true;
				console.log('您拒绝了请求',res);
				return;
		    }
		 })
	}
	//微信授权按钮获取从微信获取的用户信息中-原
	const wxGetUserInfo=(res:any,source:any='')=> {
		if(!toggClass.value&&source!='model'){
		  // this.$api.msg('请先同意"用户服务条款"再操作哦');
		  showAgr.value = true
		  canTapLogin.value = true
		  return;
		}
		if(source=='model'){
			toggClass.value=true
			showAgr.value = false
		}
		let currentAuthTime = new Date().getTime();//防止重复提交
		let timeDif = currentAuthTime - lastAuthTime.value;
		if (timeDif < 1000) {//触发时间短于1s
			return;
		}
		if (wxCode.value&&(timeDif > 1000*60*5)) {//触发时间短于1s
			getCode();
		}
		lastAuthTime.value = currentAuthTime;
		console.log('wxGetUserInfo+=',res,source)
		// this.userInfo = userInfo.detail || '';
		let {userInfo,iv,encryptedData} = res.detail;
		if (res.detail.errMsg === 'getUserInfo:ok') {
			const loginData = {
				code: wxCode.value,
				// appid: that.$global.APP_ID,
				// iv: iv,
				shopId:'10000459',
				// encryptedData: encryptedData
			};
			getMainLogin(loginData);
			
		}else if (res.detail.errMsg === 'gertUserInfo:fail system error') {
			wx.showModal({
			  title: '授权失败',
			  content: '请重新授权',
			})
		}else {
			uni.showToast({
				title: '失败信息:'+res.detail.errMsg,
				icon: 'none'
			});
		}
	}
	const getMainLogin=async(loginData:any)=>{
		// this.userParams = await this.getServerLogin(loginData);
		let t_code = await getServerLogin(loginData);
		// console.log('this.userTiket',this.userParams)
		if(t_code){
			// let {openId,unionId,shopId} = this.userParams
			let wx_info = uni.getStorageSync(global.WX_USER_INFO)||{};
			let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
			let ndata = {
				// openid:openId,
				// unionId:unionId,
				shopid:'10000459',//待检查
				nickName:wx_nickName||'',
				photo:wx_photo||'',
				code:t_code
			}
			getMemberLoginMs(ndata)
		}
		// this.getMemberTiket(ndata)
		// if(this.userParams){
		// 	this.getArchDefaultMsg();
		// 	this.getUserInfo();
		// }
	}
	const wxGetPhoneInfo=(res:any)=> {
		console.log('授权获取手机号码1:', res);
		// const that = this
		if(!this.toggClass){
		  mapi.msg('请先同意"用户服务条款"再操作哦');
		  // this.canTapLogin = true
		  return;
		}
		// 2024-06-07
		// if(!this.userParams||!this.userParams.openId){
		// 	this.canIUser = true;
		// 	that.canTapLogin = true;
		// 	return
		// }
		
		// if(!this.canTapLogin){return;}
		let currentAuthTime = new Date().getTime();//防止重复提交
		// console.log('wxGetUserProfile_showAuthorize',this.showAuthorize)
		let timeDif = currentAuthTime - this.lastPTime;
		if (timeDif < 1000) {//触发时间短于1s
			return;
		}
		if ((timeDif > 1000*60*5)) {//触发时间短于1s
			getCode();
		}
		lastPTime.value = currentAuthTime;
		// that.canTapLogin = false;
		showLoading({
			mask: true,
			title: ''
		});
		if (res.detail.errMsg !== 'getPhoneNumber:ok') {
			 hideLoading();return;
		}
		// ||res.code||res.detail.code
		 get_nphoneNumber(wxCode.value,res.detail.iv,res.detail.encryptedData)
		
		// 检查登录态是否过期
		// uni.checkSession({
		// 	success(res) {
		// 		that.get_nphoneNumber(res.detail.iv,res.detail.encryptedData)
		// 	},
		// 	fail(err) {
		// 		console.log("检查登录态是否过期:过期",err)
		// 		that.$hideLoading();
		// 	}
		// });
	}
	//获取用户手机号码（后台解密）
	const get_nphoneNumber=async(code:any,iv:any,encryptedData:any,openId:any='')=>{
		const loginData = {
			code: code,
			iv: iv,
			shopId:'10000459',
			encryptedData: encryptedData
		};
		let t_code = await getServerLogin(loginData)//登录获取code
		if(!t_code){
			canTapLogin.value = true
			hideLoading();
			return
		}
		try{
			let res=await wx_fPhone({iv: iv,newCode:code,code:t_code,encryptedData: encryptedData})
				// openId:openId,
			if (res) {
				uni.setStorageSync(global.USER_PHONE,res);//写入手机号码
				userPhone.value = res;
				actionRegist()
			} else {
				hideLoading();
				canIUser.value = true;
				canTapLogin.value = true;
				mapi.msg(res.shortMessage||'网络繁忙，稍后再试..')
				console.log('wx_fPhone获取手机号码失败_errorCount:');
			}
		}catch(err){
			hideLoading();
			canIUser.value = true;
			canTapLogin.value = true;
			mapi.msg('网络繁忙，请稍后再试...');
			console.log('wx_fPhone获取手机号码catch_request fail', err);
		}
		
				
		// this.$request
		// 	.wx_fPhone()
		// 	.then(res => {
		// 		console.log('wx_fPhone_获取用户手机号码:', res);
		// 		if (res) {
		// 			uni.setStorageSync(global.USER_PHONE,res);//写入手机号码
		// 			userPhone.value = res;
		// 			actionRegist()
		// 		} else {
		// 			hideLoading();
		// 			canIUser.value = true;
		// 			canTapLogin.value = true;
		// 			mapi.msg(res.shortMessage||'网络繁忙，稍后再试..')
		// 			console.log('wx_fPhone获取手机号码失败_errorCount:');
		// 		}
		// 	})
		// 	.catch(err => {
		// 		hideLoading();
		// 		canIUser.value = true;
		// 		canTapLogin.value = true;
		// 		mapi.msg('网络繁忙，请稍后再试...');
		// 		console.log('wx_fPhone获取手机号码catch_request fail', err);
		// 	});
	}
	const actionRegist=async(cdata:any='')=>{
		// let {openId,unionId,shopId} = this.userParams
		let wx_info = uni.getStorageSync(global.WX_USER_INFO)||'';
		let t_code = await getServerLogin(cdata);
		if(!wx_info||!t_code){
			canIUser.value = true;
			canTapLogin.value  = true;
			mapi.msg('网络繁忙，请稍后重试');
		}
		console.log('wx_info2',wx_info)
		let ndata = {
			// unionId:unionId,
			// openid:openId,
			code:t_code,
			vreg_one:"37",
			photo:wx_info.avatarUrl,
			sex:wx_info.gender==0?177:178,
			sellMemberId:shopData.value.nmember_id||uni.getStorageSync(global.QR_REGISTER_SELL_MEMBERID)||'1',
			memberType:1,
			shopid:'10000459',
			loginType:'153',
			vnick:wx_info.nickName,
			phoneNum:this.userPhone||''
		}
		registerMember(ndata)
	}
	const rezPhoneStatus=()=>{
		canIUser.value = false;
		hideLoading();
	}
	//获得登录信息-(检测该会员是否注册)
	const getMemberLoginMs=async(ndata:any)=>{
		try{
			let res:any=await getMemberLoginMst(ndata)
			if(!res.result||!res.object){
				// 去注册
				userPhone.value = uni.getStorageSync(global.USER_PHONE)||'';//写入手机号码
				
				userPhone.value?actionRegist(ndata):rezPhoneStatus();
				return
			}
			let nres = res.object
			let wx_info = uni.getStorageSync(global.WX_USER_INFO)||{};
			let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
			let {nickName,token,unionId,openid,photo,phoneNum,buyMemberId,npolicy_id} = nres
			let cacheInfo = {
				nickName:wx_nickName||nickName,
				photo:wx_photo||photo,
				phoneNum,
				buyMemberId,
				token,
				unionId,
				openid,
				npolicy_id,
				gender
			}
			setLoginCache(cacheInfo,token,true)
			mapi.msg('登录成功!');
			setTimeout(function() {
				uni.navigateBack();
			}, 600);
			hideLoading();
			console.log('cacheInfo',cacheInfo)
			// this.canTapLogin=true;//待注释
			getCode()
			// 缓存用户信息
			// if (res.result&&res.object) {
			// 	!this.phoneInfo?(this.phoneInfo = res.object.account||''):''
			// 	this.validaOldLogin(res);
			// }else {
			// 	this.validaAutoLogin(res,wechatUnionId);
			// }
		}catch(err){
			canTapLogin.value = true
			uni.setStorageSync(global.IS_LOGIN,false);
			getCode();
			hideLoading();
			mapi.msg('网络繁忙，请稍后..');
			console.log('getMemberLoginMs request fail', err);
		}
		// this.$request
		// 	.getMemberLoginMs(ndata)
		// 	.then(res => {
		// 		// let us_info = uni.getStorageSync(this.$global.WX_USER_INFO)||{};
		// 		// console.log('wx_info1',us_info)
		// 		console.log('getMemberLoginMs服务器返回内容', res);
		// 		if(!res.result||!res.object){
		// 			// 去注册
		// 			this.userPhone = uni.getStorageSync(this.$global.USER_PHONE)||'';//写入手机号码
					
		// 			this.userPhone?this.actionRegist(ndata):this.rezPhoneStatus();
		// 			return
		// 		}
		// 		// 暂=>
		// 		// this.canIUser = false
		// 		// this.$hideLoading();
		// 		// return
		// 		// 暂=>
		// 		let nres = res.object
		// 		let wx_info = uni.getStorageSync(this.$global.WX_USER_INFO)||{};
		// 		let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
		// 		let {nickName,token,unionId,openid,photo,phoneNum,buyMemberId,npolicy_id} = nres
		// 		let cacheInfo = {
		// 			nickName:wx_nickName||nickName,
		// 			photo:wx_photo||photo,
		// 			phoneNum,
		// 			buyMemberId,
		// 			token,
		// 			unionId,
		// 			openid,
		// 			npolicy_id,
		// 			gender
		// 		}
		// 		this.setLoginCache(cacheInfo,token,true)
		// 		this.$api.msg('登录成功!');
		// 		setTimeout(function() {
		// 			uni.navigateBack();
		// 		}, 600);
		// 		this.$hideLoading();
		// 		console.log('cacheInfo',cacheInfo)
		// 		// this.canTapLogin=true;//待注释
		// 		this.getCode()
		// 		// 缓存用户信息
		// 		// if (res.result&&res.object) {
		// 		// 	!this.phoneInfo?(this.phoneInfo = res.object.account||''):''
		// 		// 	this.validaOldLogin(res);
		// 		// }else {
		// 		// 	this.validaAutoLogin(res,wechatUnionId);
		// 		// }
		// 	})
		// 	.catch(err => {
		// 		this.canTapLogin = true
		// 		uni.setStorageSync(this.$global.IS_LOGIN,false);
		// 		this.getCode();
		// 		this.$hideLoading();
		// 		this.$api.msg('网络繁忙，请稍后..');
		// 		console.log('getMemberLoginMs request fail', err);
		// 	});
		
	}
	const setLoginCache=(cacheInfo:any,token:any,isLogin:any)=>{
		uni.setStorageSync(global.USER_INFO, cacheInfo);
		uni.setStorageSync(global.USER_TIKET, token);
		uni.setStorageSync(global.IS_LOGIN,isLogin);
	}
	const clearnCache=()=>{
		uni.setStorageSync(global.USER_INFO, '');
		uni.setStorageSync(global.USER_TIKET, '');
		uni.setStorageSync(global.IS_LOGIN,false);
	}
	// 注册
	const registerMember=async(ndata:any)=>{
		try{
			let res=await registerMembert(ndata)
			if (!res.result||!res.object) {
				canTapLogin.value = true;
				canIUser.value = true;
				mapi.msg(res.shortMessage||'登录异常，请稍后..');
				getCode();
				clearnCache()
				return;
			}
			let nres = res.object
			let wx_info = uni.getStorageSync(global.WX_USER_INFO)||{};
			let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
			let {nickName,token,unionId,openid,photo,phoneNum,buyMemberId,npolicy_id} = nres
			let cacheInfo = {
				nickName:wx_nickName||nickName,
				photo:wx_photo||photo,
				phoneNum,
				buyMemberId,
				token,
				unionId,
				openid,
				npolicy_id,
				gender
			}
			console.log('cacheInfo',cacheInfo)
			setLoginCache(cacheInfo,token,true)
			mapi.msg('登录成功!');
			setTimeout(function() {
				uni.navigateBack();
			}, 600);
		}catch(err){
			//TODO handle the exception
			clearnCache()
			canTapLogin.value=true;
			canIUser.value = true;
			getCode();
			// mapi.msg(err||'登录异常，请稍后.');
			hideLoading();
			// this.$wxlg('err','注册时param'+ndata);
		}
		// this.$request
		// .registerMember(ndata)
		// .then(res => {
		// 	this.$hideLoading();
		// 	console.log('registerMember请求返回', res);
		// 	if (!res.result||!res.object) {
		// 		this.canTapLogin = true;
		// 		this.canIUser = true;
		// 		this.$api.msg(res.shortMessage||'登录异常，请稍后..');
		// 		this.getCode();
		// 		this.clearnCache()
		// 		return;
		// 	}
		// 	let nres = res.object
		// 	let wx_info = uni.getStorageSync(this.$global.WX_USER_INFO)||{};
		// 	let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
		// 	let {nickName,token,unionId,openid,photo,phoneNum,buyMemberId,npolicy_id} = nres
		// 	let cacheInfo = {
		// 		nickName:wx_nickName||nickName,
		// 		photo:wx_photo||photo,
		// 		phoneNum,
		// 		buyMemberId,
		// 		token,
		// 		unionId,
		// 		openid,
		// 		npolicy_id,
		// 		gender
		// 	}
		// 	console.log('cacheInfo',cacheInfo)
		// 	this.setLoginCache(cacheInfo,token,true)
		// 	this.$api.msg('登录成功!');
		// 	setTimeout(function() {
		// 		uni.navigateBack();
		// 	}, 600);
		// })
		// .catch(err => {
		// 	this.clearnCache()
		// 	this.canTapLogin=true;
		// 	this.canIUser = true;
		// 	this.getCode();
		// 	this.$api.msg(err||'登录异常，请稍后.');
		// 	this.$hideLoading();
		// 	this.$wxlg('err','注册时param'+ndata);
		// 	// resolve('');
		// 	console.log('registerMember请求返回request fail', err);
		// });
		
	}
	const getServerLogin=(loginData:any)=>{
		return new Promise(async(resolve,reject) =>{
			let ocd = uni.getStorageSync('t_code')||{};
			if(ocd.code&&ocd.time&& (ocd.time - (new Date().getTime())>0)){
				resolve(ocd.code);
				return 
			}
			showLoading({
				title: '',
				mask: true,
			});
			let res=await getServerLogint(loginData)
			try{
				if (!res.result) {
					hideLoading();
					canTapLogin.value=true;
					mapi.msg(res.shortMessage||'网络繁忙，请稍后...');
					getCode();
					resolve('');
					// this.$wxlg('info','登录时getServerLogin'+res);
					return;
				}
				let tcd = {
					code:res.object||'',
					time:(new Date().getTime()+4*60*1000)
				}
				uni.setStorageSync('t_code',tcd)
				
				resolve(tcd.code)
			}catch(err){
				canTapLogin.value=true;
				getCode();
				// mapi.msg(err||'网络繁忙，请稍后.');
				hideLoading();
				// this.$wxlg('err','登录时getServerLogin'+err);
				resolve('');
			}
		// 	this.$request
		// 	.getServerLogin(loginData)
		// 	.then(res => {
		// 		console.log('getServerLogin请求返回', res);
		// 		if (!res.result) {
		// 			this.$hideLoading();
		// 			this.canTapLogin=true;
		// 			this.$api.msg(res.shortMessage||'网络繁忙，请稍后...');
		// 			this.getCode();
		// 			resolve('');
		// 			// this.$wxlg('info','登录时getServerLogin'+res);
		// 			return;
		// 		}
		// 		// let nuserParams = res||'';
		// 		let tcd = {
		// 			code:res.object||'',
		// 			time:(new Date().getTime()+4*60*1000)
		// 		}
		// 		uni.setStorageSync('t_code',tcd)
				
		// 		resolve(tcd.code)
				
		// 	})
		// 	.catch(err => {
				
		// 		this.canTapLogin=true;
		// 		this.getCode();
		// 		this.$api.msg(err||'网络繁忙，请稍后.');
		// 		this.$hideLoading();
		// 		// this.$wxlg('err','登录时getServerLogin'+err);
		// 		resolve('');
		// 		console.log('getServerLogin请求返回request fail', err);
		// 	});
		})
		
	}
	// 退出登录
	const wxLoginOut=async()=> {
		getCode();
		let cacheIfo = uni.getStorageSync(global.USER_INFO)||{};
		let {buyMemberId,phoneNum} = cacheIfo
		let ndata = {
			code:wxCode.value || '',
			account:phoneNum,
			groupMemberId:'',
			memberId:buyMemberId,
			vcard:'',
			// openId:openid,
			// unionId:unionId
		}
		try{
			let res= await wxLoginOutt(ndata)
			if (!res.result) {
				mapi.msg(res.shortMessage||'网络繁忙，请稍后..');
				return;
			}
		    clearnCache()
		}catch(e){
			hideLoading();
		  mapi.msg('请求失败');
		}
		// this.$request
		// 	.wxLoginOut(ndata)
		// 	.then(res => {
		// 		this.$hideLoading();
		// 		console.log('wxLoginOut服务器返回内容', res);
		// 		if (!res.result) {
		// 			this.$api.msg(res.shortMessage||'网络繁忙，请稍后..');
		// 			return;
		// 		}
		// 		this.clearnCache()
				
		// 	})
		// 	.catch(err => {
		// 		// this.canTapLogin = true
		// 		this.$hideLoading();
		// 		this.$api.msg('请求失败');
		// 		console.log('wxLoginOut request fail', err);
		// 	});
	}
	//获取会员缓存数据
	const getMemberTiket=async(ndata:any)=> {
		// const wechatUnionId = memberUnionId=='' ? this.authUnionId : memberUnionId;
		// { tiket: this.serverTokenId,unionId:wechatUnionId,paytel:this.phoneInfo}
		showLoading({
			title: '',
			mask: true,
		});
		try{
			let res=await member_tikett(ndata)
			hideLoading();
			console.log('getMemberTiket服务器返回内容', res);
			if (!res.result||!res.object) {
				canTapLogin.value = true
				return;
			}
			let nres = res.object||{};
			let wx_info = uni.getStorageSync(global.WX_USER_INFO)||{};
			let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
			// unionId,openId:openid,2024-06-07
			let {nickName,tiket:token,avatarUrl:photo,account:phoneNum,memberId:buyMemberId,policyId:npolicy_id} = nres
			if(!token){
				canTapLogin.value = true
				return
			}
			let cacheInfo = {
				nickName:wx_nickName||nickName,
				photo:wx_photo||photo,
				phoneNum,
				buyMemberId,
				token,
				// unionId,
				// openid,
				npolicy_id,
				gender
			}
			console.log('cacheInfo_tiket',cacheInfo)
			 setLoginCache(cacheInfo,token,true)
			mapi.msg('登录成功!');
			setTimeout(function() {
				uni.navigateBack();
			}, 600);
		}catch(err){
			//TODO handle the exception
			canTapLogin.value = true
			hideLoading();
			// mapi.msg(err||'');
		}
		// this.$request
		// 	.member_tiket(ndata)
		// 	.then(res => {
		// 		this.$hideLoading();
		// 		console.log('getMemberTiket服务器返回内容', res);
		// 		if (!res.result||!res.object) {
		// 			this.canTapLogin = true
		// 			return;
		// 		}
		// 		let nres = res.object||{};
		// 		let wx_info = uni.getStorageSync(this.$global.WX_USER_INFO)||{};
		// 		let {nickName:wx_nickName,avatarUrl:wx_photo,gender} = wx_info
		// 		// unionId,openId:openid,2024-06-07
		// 		let {nickName,tiket:token,avatarUrl:photo,account:phoneNum,memberId:buyMemberId,policyId:npolicy_id} = nres
		// 		if(!token){
		// 			this.canTapLogin = true
		// 			return
		// 		}
		// 		let cacheInfo = {
		// 			nickName:wx_nickName||nickName,
		// 			photo:wx_photo||photo,
		// 			phoneNum,
		// 			buyMemberId,
		// 			token,
		// 			// unionId,
		// 			// openid,
		// 			npolicy_id,
		// 			gender
		// 		}
		// 		console.log('cacheInfo_tiket',cacheInfo)
		// 		this.setLoginCache(cacheInfo,token,true)
		// 		this.$api.msg('登录成功!');
		// 		setTimeout(function() {
		// 			uni.navigateBack();
		// 		}, 600);
		// 	})
		// 	.catch(err => {
		// 		this.canTapLogin = true
		// 		this.$hideLoading();
		// 		this.$api.msg(err||'');
		// 		console.log('getMemberTiket request fail', err);
		// 	});
	}
	const goBackPage=()=>{
		uni.navigateBack({})
	}
	const showModalService=()=>{
		modalSName.value = 'show'
	}
	const hideModalService=()=>{
		modalSName.value = ''
	}
	const toggleTips=()=>{
		toggClass.value = !toggClass.value;
	}
	const stopPrevent=()=>{}
	const getCode=()=>{
		wx.login({
			provider: 'weixin',
			success: loginRes => {
				console.log('getCode001',loginRes)
				wxCode.value = loginRes.code;
			},
			fail:err => {
				console.log('wx.login-err',err)
			}
		});
	}
</script>

<style lang="scss" scoped>
page {
	background-color: #F4F4F4;
}
.contain{
	width: 100vw;
	height: 100vh;
}
.auth_top{
	flex: 1.3;
	padding: 90rpx 20rpx 0 60rpx;
	.logoBox{
		.logo{
			display: inline-block;
			width: 68rpx;
			height: 68rpx;
			font-size: 63rpx;
			border-radius: 50%;
			background: #46B79B;
			color: #fff;
			line-height: 70rpx;
			text-align: center;
		}
		.econicon-test1{
			font-size: 70rpx;
		}
	}
	.comText{
		margin-top: 20rpx;
	}
	.welText{
		font-family:'PingFang SC';
		font-weight: bold;
		font-size: 56rpx;
		color: #323232;
	}
	.welDis{
		margin-top: 20rpx;
		color: #818181;
		font-size: 30rpx;
	}
}
.authFooteSection{
	flex: 1;
	.nextStep {
		display: block;
		height: 80rpx;
		line-height: 79rpx;
		margin: 0 60rpx;
		background: linear-gradient(90deg, rgba(40, 176, 91, 1) 0%, rgba(70, 183, 155, 1) 100%);
		font-size: 30rpx;
		// line-height: 85rpx;
		box-shadow: 0px 4rpx 18rpx rgba(0, 0, 0, 0.08);
		font-family: Hiragino Sans GB;
		color: #ffffff;
		text-align: center;
		border:0;
		border-radius: 6rpx;
		overflow: hidden;
		&::after{
			border: 0;
		}
		.econwechat{
			font-size: 34rpx;
			color: #fff;
		}
	}
	.cancel{
		height: 80rpx;
		line-height: 80rpx;
		margin:32rpx 60rpx;
		background-color: #fff;
		box-shadow: 0px 4rpx 18rpx rgba(0, 0, 0, 0.08);
		border-radius: 6rpx;
		text-align: center;
		font-size: 30rpx;
		color: #999;
		letter-spacing: 6rpx;
	}
	.tips{
		margin-left: 68rpx;
		font-size: 24rpx;
		.tipIco{
			margin-bottom:-1rpx;
			font-size: 26rpx;
			&::before{
				color: #999;
			}
		}
	}
}
.loginFooteSection {
	flex: 1;
	.btnBox{
		margin: 0 60rpx;
	}
	.nextStep {
		width: 294rpx;
		height: 80rpx;
		margin: 0;
		background: linear-gradient(90deg, rgba(40, 176, 91, 1) 0%, rgba(70, 183, 155, 1) 100%);
		font-size: 30rpx;
		// line-height: 85rpx;
		box-shadow:0px 10px 18px 0px rgba(0, 0, 0, 0.1);
		font-family: Hiragino Sans GB;
		color: #ffffff;
		text-align: center;
		border:0;
		border-radius: 6rpx;
		overflow: hidden;
		&::after{
			border: 0;
		}
		.econwechat{
			font-size: 34rpx;
			color: #fff;
		}
	}
	.cancel{
		width: 294rpx;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #fff;
		box-shadow:0px 10px 18px 0px rgba(0, 0, 0, 0.1);
		border-radius: 6rpx;
		text-align: center;
		font-size: 30rpx;
		color: #999;
		letter-spacing: 6rpx;
	}
	.tips{
		margin-top: 30rpx;
		margin-left: 68rpx;
		font-size: 24rpx;
		.tipIco{
			margin-bottom:-1rpx;
			font-size: 26rpx;
			&::before{
				color: #999;
			}
		}
	}
}
.popup{
	.nextStep {
		height: 85rpx;
		margin: 140rpx 48rpx 68rpx;
		background: linear-gradient(90deg, rgba(40, 176, 91, 1) 0%, rgba(70, 183, 155, 1) 100%);
		font-size: 30rpx;
		line-height: 85rpx;
		font-family: Hiragino Sans GB;
		color: #ffffff;
		text-align: center;
		border-radius: 6rpx;
	}
	// .image {
	// 	width: 280rpx;
	// 	height: 280rpx;
	// 	// margin-top: 20%;
	// }
	.title_image {
		width: 38.5rpx;
		height: 39rpx;
		margin-left: 25rpx;
	}
	
	.title {
		line-height: 77rpx;
		font-size: 33rpx;
		color: #000000;
		font-family: PingFang SC;
		font-weight: bold;
		margin-left: 25rpx;
		// text-align: left;
	}
	
	.active {
		// background-color: #F0F0F0;
		margin: 20rpx 25rpx 0;
		height: 100rpx;
		border-bottom: 1px solid rgba(232, 232, 232, 1);
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 100rpx;
		padding-left: 40rpx;
		// text-align: left;
	}
	
	.active_no {
		color: #666666;
		font-weight: normal;
	}
	
}
.gougou {
	font-size: 50rpx;
	color: #40b68f;
	margin-right: 48rpx;
	font-weight: normal;
}
.modalTitle {
	color: #000000;
	font-size: 32rpx;
	font-weight: bold;
	font-family: PingFang SC;
	line-height: 120rpx;
	height: 120rpx;
	width: 100%;
}
.imgCloseBtn {
	position: absolute;
	top: 40rpx;
	left: 46rpx;
	width: 40rpx;
	height: 40rpx;
	color: #999999;
	font-size: 36rpx;
	font-weight: 100;
}
.modalContent {
	margin: 0 46rpx 50rpx;
	color: #333333;
	font-size: 24rpx;
	line-height: 36rpx;
	text-align: left;
}
.cu-dialog.serv{
	width: 610rpx;
}
// 服务协议
.cu-dialog.agr{
	width: 526rpx;
}
.agrContent{
	width: 526rpx;
	min-height: 360rpx;
	border-radius: 24rpx;
}
.agrbtn{
	width: 210rpx;
	height: 80rpx;
	line-height: 81rpx;
	border-radius: 10rpx;
	border: 1px solid #56BF7A;
	&::after{
		border: none;
	}
	&.full{
		background: #56BF7A;
		color: #fff;
	}
}
.cu-modal.zindex{
	z-index: 1116;
}
.servContent{
	max-height: 80vh;
	overflow-y: scroll;
	padding: 45rpx;
	-webkit-overflow-scrolling: touch;
	.sboxMain{
		color: #333;
		text-align: left
	}
}
</style>